package com.hdaccp.gymxmjpa.Controller;
import com.hdaccp.gymxmjpa.Dao.GoodsDao;
import com.hdaccp.gymxmjpa.Service.GoodsDaoImpl;
import com.hdaccp.gymxmjpa.entity.Coach;
import com.hdaccp.gymxmjpa.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/goods")
public class GoodsController {
   @Autowired
    private GoodsDao goodsDao;
   @Autowired
   private GoodsDaoImpl goodsDaoImpl;

    @RequestMapping("/sp")
    public String sp(){

        return "WEB-INF/jsp/Goods";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(String goodsname, int pageSize, int pageNumber){
        Map<String,Object>  map1=new HashMap<String,Object>();
        map1.put("goodsname",goodsname);
        map1.put("qi",(pageNumber-1)*pageSize);
        map1.put("shi",pageSize);
        return goodsDaoImpl.query(map1);
    }
    @RequestMapping("/query2")
    @ResponseBody
    public List<Goods> query2(){
        List list  = goodsDao.findAll();
        return list;
    }
 @RequestMapping("/del")
    @ResponseBody
    public  Map<String,Object> del(long goodsId,String goodsname, int pageSize, int pageNumber){
     goodsDao.deleteById(goodsId);
     Map<String,Object>  map1=new HashMap<String,Object>();
     map1.put("goodsname",goodsname);
     map1.put("qi",(pageNumber-1)*pageSize);
     map1.put("shi",pageSize);
     return goodsDaoImpl.query(map1);
    }
    @RequestMapping("/add")
    @ResponseBody
    public  void save(Goods goods){
        goodsDao.save(goods);
    }
    @RequestMapping("/cha")
    @ResponseBody
    public Optional<Goods> one(long goodsId){
        return goodsDao.findById(goodsId);
    }
    @RequestMapping("/count")
    @ResponseBody
    public Long count (String goodsName){
        goodsDaoImpl.count(goodsName);
        return  goodsDaoImpl.count(goodsName);
    }
    @RequestMapping("/upd")
    @ResponseBody
    public  void upd(Goods goods){
        goodsDaoImpl.update(goods);
    }
    @RequestMapping("/update")
    @ResponseBody
    public  void update(Goods goods){
        goodsDao.save(goods);
    }
    @RequestMapping("/topcoach")
    @ResponseBody
    public Map<String,Object> topcoach(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("goods",goodsDao.findAll());
        return map ;
    }
}
